﻿using System;
using FluentMigrator;
using FluentMigrator.Builders.Create.Table;
using Zinnia.Lite.Entity;

namespace Zinnia.Lite.Migrations
{
    /// <summary>
    /// 数据迁移基类
    /// </summary>
    public abstract class ZinniaBaseMigrator<T> : Migration where T : ZinniaObject
    {
        protected Type GenericType
        {
            get
            {
                return typeof(T);
            }
        }

        public virtual string TableName()
        {
            return GenericType.FullName.Substring(GenericType.FullName.LastIndexOf('.') + 1);
        }
        public virtual string SchemaName()
        {
            return "dbo";
        }

        public ICreateTableColumnOptionOrWithColumnSyntax InitTable()
        {
            return Create.Table(SchemaName() + TableName())
                .WithColumn("Id").AsString().PrimaryKey()
                .WithColumn("CreateTimeStamp").AsDateTime();
        }

        public bool IsExistTable()
        {
            return Schema.Table(SchemaName() + TableName()).Exists();
        }
    }
}
